﻿using AutoMan.Helper;
using AutoMan.Models;
using DevExpress.Charts.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMan.Utils
{
    public class CommonUtil
    {
        public static void InitDatabase()
        {
            SqlsugarHelper.Db.DbMaintenance.CreateDatabase();
            Type[] types = EntityHelper.GetEntityInfo();
            SqlsugarHelper.Db.CodeFirst.InitTables(types);
            var formModel=SqlsugarHelper.Db.Insertable(new FormModel()
            {
                名称="字段管理",
                别名="FormFieldModel"
            }).ExecuteReturnEntity();
            SqlsugarHelper.Db.Insertable(
                new List<FormFieldModel>() {
                    new()
                    {
                        表单编号= formModel.编号,
                        名称= "编号",
                        别名= "Id",
                        类型= "TextEdit",
                        外键表名= "",
                        描述=""
                    },
                    new()
                    {
                        表单编号 = formModel.编号,
                        名称 = "表单编号",
                        别名 = "FormId",
                        类型 = "TextEdit",
                        外键表名 = "FormModel",
                        描述 = ""
                    }
                }
            ).ExecuteCommand();
            ConfigSave("Installed", "true");
        }
        public static string ConfigReader(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public static void ConfigSave(string key, string value)
        {
            try
            {
                // 打开当前应用程序的配置文件
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                // 修改配置值
                config.AppSettings.Settings[key].Value = value;

                // 保存更改
                config.Save(ConfigurationSaveMode.Modified);

                // 刷新 appSettings 部分，确保新的值被加载到内存中
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}");
            }
        }
        /// <summary>
        /// 根据条件移除List<dynamic>字段
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="strings"></param>
        /// <returns></returns>
        public static List<dynamic> RemoveListDynamicItem(List<dynamic> lists, List<string> strings)
        {
            foreach (var item in lists)
            {
                if (item is ExpandoObject expando)
                {
                    foreach (var str in strings)
                    {
                        ((IDictionary<string, object>)expando).Remove(str);
                    }
                }
            }
            return lists;
        }
    }
}
